<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${dataItem.machineName || '无'}-能耗明细`"
		width="800px"
		append-to-body
		:show-confirm-btn="false"
		:show-cancel-btn="false"
		:show-close-btn="true"
		@on-visible-change="onVisibleChange"
		@on-close="onVisibleChange(false)"
	>
		<header ref="box1" class="margin-bottom-10 flex-center">
			<el-button size="small" type="primary">
				<span style="font-size: 14px;">{{ dataItem.productName || '无' }}</span>
			</el-button>
		</header>

		<jk-table
			ref="jkTable"
			:loading="tableLoading"
			class="width-100-percent"
			:data="tableData"
			border
			highlight-hover-row
			highlight-current-row
			:height="dialogTableHeight - 30"
		>
			<vxe-table-column type="seq" title="序号" align="center" :width="50"></vxe-table-column>
			<vxe-table-column field="startDateTime" title="开始时间" align="left" :width="150"></vxe-table-column>
			<vxe-table-column field="endDateTime" title="结束时间" align="left" :width="150"></vxe-table-column>
			<vxe-table-column field="energy" title="每落纱能耗(kw·h)" align="right" :min-width="120"></vxe-table-column>
			<vxe-table-column field="oneHundredKilogramsEnergy" title="每100公斤能耗(kw·h)" align="right" :min-width="160"></vxe-table-column>
		</jk-table>
		<footer ref="box3" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="totalCount"
				layout="total, prev, pager, next"
				@on-change="onPageChange"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '../../../mixins/cal-dialog-height';
    import { pageConfig } from '../../../utils/jk/jk-tool';
    import {
        productionMachineTonYarnEnergyMachinePage
    } from '../../../api/production/reports';

    export default {
        mixins: [calDialogHeight],
        props: {
            dataItem: {
                type: Object,
                default() {
                    return {};
                }
            },
            dialogState: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize
                },
                tableData: [],
                showDialog: false,
                tableLoading: false,
                totalCount: null
            };
        },
        methods: {
            onCustomTimeChange(e) {
                if (e) {
                    this.$set(this.searchParams, 'belongDate', null);
                    this.$set(this.searchParams, 'shiftId', null);
                } else {
                    this.$set(this.searchParams, 'dateTimeRange', []);
                }
            },
            onCellDblclick(e) {
                this.$emit('on-confirm', e.row);
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            getListRequest() {
                this.tableLoading = true;
                if (!this.dataItem.workshopId || !this.dataItem.belongDate || !this.dataItem.scheduleShiftId || !this.dataItem.machineId) return false;
                return productionMachineTonYarnEnergyMachinePage({
                    workshopId: this.dataItem.workshopId,
                    belongDate: this.dataItem.belongDate,
                    scheduleShiftId: this.dataItem.scheduleShiftId,
                    machineId: this.dataItem.machineId
                }).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.totalCount = res.data.count;
                        this.tableData = res.data.res;
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            async getDependentData() {
                this.tableLoading = true;
                await this.getListRequest();
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>

